package com.woniuxy.bases.service.impl;

import com.woniuxy.bases.entity.Lines;
import com.woniuxy.bases.entity.dto.LineGetDTO;
import com.woniuxy.bases.entity.dto.LinesAddDTO;
import com.woniuxy.bases.entity.dto.LinesUpdateDTO;
import com.woniuxy.bases.mapper.LinesMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bases.service.LinesService;
import com.woniuxy.comments.util.CustomPage;
import com.woniuxy.comments.util.ResponseResult;
import com.woniuxy.comments.util.ResponseState;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LinesServiceImpl implements LinesService {
    @Resource
    private LinesMapper linesMapper;

    @Override
    public ResponseResult<CustomPage<List<Lines>>> getAllLines(LineGetDTO lineGetDTO) {
        PageHelper.startPage(lineGetDTO.getPage().getPageIndex(), lineGetDTO.getPage().getPageSize());
        List<Lines> list = linesMapper.getAllLines(lineGetDTO);
        PageInfo<Lines> pageInfo = new PageInfo<>(list);
        CustomPage<List<Lines>> customPage = new CustomPage<>();
        customPage.setTotalPage(pageInfo.getPages());
        customPage.setCurrentPage(pageInfo.getPageNum());
        customPage.setSize(pageInfo.getSize());
        customPage.setData(list);
        customPage.setTotal(pageInfo.getTotal());
        return new ResponseResult<CustomPage<List<Lines>>>()
                .setCode(200)
                .setMessage("true")
                .setData(customPage)
                .setState(ResponseState.SELECT_SUCCESS);
    }

    @Override
    public boolean banLines(List list) {
        return linesMapper.banLines(list);
    }

    @Override
    public boolean addNewLines(LinesAddDTO linesAddDTO) {
        //获取上一个的id，到这里加一
        Lines lines = linesMapper.getLatestLine();
        linesAddDTO.setLineCode("AS"+lines.getLineId()+1);
        return linesMapper.addNewLines(linesAddDTO);
    }

    @Override
    public boolean delLines(List list) {
        return linesMapper.delLines(list);
    }

    @Override
    public boolean updateLines(LinesUpdateDTO linesUpdateDTO) {
        if (linesMapper.getLineById(linesUpdateDTO.getLineId())) {
            return linesMapper.updateLines(linesUpdateDTO);
        }
        return false;
    }
}
